package com.qf.gh.dao.impl;

import com.qf.gh.dao.GoodsTypeDao;
import com.qf.gh.domain.GoodsType;
import com.qf.gh.utils.DateSourceUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class GoodsTypeDaoImpl implements GoodsTypeDao {
    @Override
    public List<GoodsType> selectByLevel(int i) {
        QueryRunner qr=new QueryRunner(DateSourceUtils.getDataSource());
        String sql="select  id, name ,level , Parent from tb_goods_type where level = ? ";
        try {
            List<GoodsType> goodsTypes = qr.query(sql, new BeanListHandler<>(GoodsType.class),i);
            return goodsTypes;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public GoodsType selectById(int typeid) {
        QueryRunner qr=new QueryRunner(DateSourceUtils.getDataSource());
        String sql="select  id, name ,level , Parent from tb_goods_type where id = ? ";
        try {
            GoodsType goodsTypes = (GoodsType) qr.query(sql, new BeanHandler<>(GoodsType.class),typeid);
            return goodsTypes;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<GoodsType> selectAll() {
        QueryRunner qr=new QueryRunner(DateSourceUtils.getDataSource());
        String sql="select  id, name ,level , Parent from tb_goods_type ";
        try {
            List<GoodsType> goodsTypes = qr.query(sql, new BeanListHandler<>(GoodsType.class));
            return goodsTypes;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void add(GoodsType goodsType) {
        QueryRunner qr=new QueryRunner(DateSourceUtils.getDataSource());
        String sql="insert into tb_goods_type values(null,?,?,?) ";
        try {
            qr.update(sql,goodsType.getName(),goodsType.getLevel(),goodsType.getParent());
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("新增商品类别失败");
        }
    }

    @Override
    public void delete(int parseInt) {
        String sql="delete from tb_goods_type where id=?";
        QueryRunner qr=new QueryRunner(DateSourceUtils.getDataSource());
        try {
            qr.update(sql,parseInt);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("删除商品类型失败");
        }
    }

    @Override
    public void update(GoodsType goodsType1) {

        String sql="update tb_goods_type set name=?,level=?,Parent=? where id=?";
        QueryRunner qr=new QueryRunner(DateSourceUtils.getDataSource());
        try {
            qr.update(sql,goodsType1.getName(),goodsType1.getLevel(),goodsType1.getParent(),goodsType1.getId());
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("修改商品类型失败");
        }
    }

    @Override
    public List<GoodsType> selectByName(String toString, List<Object> parmas) {
        QueryRunner qr=new QueryRunner(DateSourceUtils.getDataSource());
        String sql="select  id, name ,level , Parent from tb_goods_type "+toString;
        try {
            List<GoodsType> goodsTypes = qr.query(sql, new BeanListHandler<>(GoodsType.class),parmas.toArray());
            return goodsTypes;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
